package pri.syl.xiaowu.websocket.netty.core.factory;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import pri.syl.xiaowu.websocket.netty.core.factory.constant.ThreadPoolConstant;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author YunLong
 * @Date 2021/7/18 18:56
 * @Description 线程池工厂
 **/
public class ThreadPoolFactory {
    private ThreadPoolFactory() {
    }

    public static ThreadPoolExecutor createHttpServerHandshakeThreadPool() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat(ThreadPoolConstant.HTTP_SERVER_HANDSHAKE).build();
        return new ThreadPoolExecutor(ThreadPoolConstant.HTTP_SERVER_HANDSHAKE_CORE_POOL_SIZE,
                ThreadPoolConstant.HTTP_SERVER_HANDSHAKE_MAXIMUM_POOL_SIZE,
                ThreadPoolConstant.HTTP_SERVER_HANDSHAKE_KEEP_ALIVE_TIME,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(),
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    public static ThreadPoolExecutor createWebsocketServerBusinessThreadPool() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat(ThreadPoolConstant.WEBSOCKET_SERVER_BUSINESS).build();
        return new ThreadPoolExecutor(ThreadPoolConstant.WEBSOCKET_SERVER_BUSINESS_CORE_POOL_SIZE,
                ThreadPoolConstant.WEBSOCKET_SERVER_BUSINESS_MAXIMUM_POOL_SIZE,
                ThreadPoolConstant.WEBSOCKET_SERVER_BUSINESS_KEEP_ALIVE_TIME,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(),
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    public static ThreadPoolExecutor createWebsocketServerInitThreadPool() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat(ThreadPoolConstant.WEBSOCKET_SERVER_INIT).build();
        return new ThreadPoolExecutor(ThreadPoolConstant.WEBSOCKET_SERVER_INIT_CORE_POOL_SIZE,
                ThreadPoolConstant.WEBSOCKET_SERVER_INIT_MAXIMUM_POOL_SIZE,
                ThreadPoolConstant.WEBSOCKET_SERVER_INIT_KEEP_ALIVE_TIME,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(),
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
